import {clone, isNumber} from 'xe-utils';

function CopyObj (target, original){
	if (original) {
		//解除obs
		original = clone(original, true);
		Object.keys(target).forEach(key => {
			target[key] = original[key];
		});
	}
}

export const ArrToObj = (target, getKey, getVal) => {
	let obj = {};
	if (target) {
		for (const item of target) {
			obj[getKey(item)] = getVal ? getVal(item) : item;
		}
	}
	return obj;
}


/**
 * 获取当前时间
 */

function getLaterDate(dayNum, dateTime = null) {
	// 如果为null,则格式化当前时间为时间戳
	if (!dateTime) dateTime = +new Date();
	// 如果dateTime长度为10或者13，则为秒和毫秒的时间戳，如果超过13位，则为其他的时间格式
	if (dateTime.toString().length == 10) dateTime *= 1000;
	const timestamp = +new Date(Number(dateTime));

	const one_day = 86400000; // 24 * 60 * 60 * 1000;
	const addVal = dayNum * one_day + timestamp;
	//x天后的日期
	const date = new Date(addVal);

	//格式化日期
	const filters = n => {
		return n < 10 ? (n = '0' + n) : n;
	};
	const month = filters(date.getMonth() + 1);
	const day = filters(date.getDate());
	const hours = filters(date.getHours());
	const minutes = filters(date.getMinutes());
	const seconds = filters(date.getSeconds());
	// const lastTime = `${date.getFullYear()}-${month}-${day}`;
	const lastTime = `${date.getFullYear()}年${month}期`;
	return lastTime;
}


module.exports = {
	getLaterDate,
	CopyObj
};